package com.ibm.cps.sql;

import net.sf.jsqlparser.statement.StatementVisitorAdapter;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.factories.DataSourceFactory;
import com.ibm.util.SchemaCache;

/**
 * Created by telekinesis on 6/4/15.
 */
public class StreamingSqlVisitor extends StatementVisitorAdapter {

	private final ObjectNode sqlMeta;
	private final String userId;
	private final DataSourceFactory datasourceFactory;
	private final SchemaCache schemaCache;
	private SelectBodyVisitor visitor;

	public StreamingSqlVisitor(ObjectNode sqlMeta, String userId, DataSourceFactory datasourceFactory, SchemaCache schemaCache) {
		this.sqlMeta = sqlMeta;
		this.userId = userId;
		this.datasourceFactory = datasourceFactory;
		this.schemaCache = schemaCache;
	}

	@Override
	public void visit(Select select) {
		SelectBody body = select.getSelectBody();
		visitor = new SelectBodyVisitor(sqlMeta, userId, datasourceFactory, schemaCache);
		body.accept(visitor);
	}

	public ArrayNode getResultingMetaData() {
		return visitor.getResult();
	}
}
